//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jeesite.modules.sys.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.dao.OfficeDao;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.OfficeService;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.sys.web.user.EmpUserController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"${adminPath}/sys/office"})
@ConditionalOnProperty(
        name = {"web.core.enabled"},
        havingValue = "true",
        matchIfMissing = true
)
public class OfficeController extends BaseController {
    @Autowired
    private OfficeService officeService;
    @Autowired
    private EmpUserController empUserController;
    @Autowired
    private OfficeDao officeDao;

    public OfficeController() {
    }

    @ModelAttribute
    public Office get(String officeCode, boolean isNewRecord) {
        return (Office)this.officeService.get(officeCode, isNewRecord);
    }

    @RequiresPermissions({"sys:office:view"})
    @RequestMapping({"list"})
    public String list(Office office, Model model) {
        model.addAttribute("office", office);
        return "modules/sys/officeList";
    }

    @RequiresPermissions({"user"})
    @RequestMapping({"listData"})
    @ResponseBody
    public List<Office> listData(Office office, String ctrlPermi) {
        if(StringUtils.isBlank(office.getParentCode())) {
            office.setParentCode("0");
        }

        if(StringUtils.isNotBlank(office.getViewCode()) || StringUtils.isNotBlank(office.getOfficeName()) || StringUtils.isNotBlank(office.getFullName())) {
            office.setParentCode((String)null);
        }

        this.officeService.addDataScopeFilter(office, ctrlPermi);
        List list = this.officeService.findList(office);
        return list;
    }

    @RequiresPermissions({"sys:office:view"})
    @RequestMapping({"form"})
    public String form(Office office, Model model) {
        office = this.createNextNode(office);
        if(StringUtils.isNotBlank(office.getParentCode())) {
            office.setParent((Office)this.officeService.get(office.getParentCode()));
        }

        if(office.getIsNewRecord()) {
            office.setTreeSort(Integer.valueOf(30));
            Office where = new Office();
            where.setParentCode(office.getParentCode());
            Office last = (Office)this.officeService.getLastByParentCode(where);
            if(last != null) {
                office.setTreeSort(Integer.valueOf(last.getTreeSort().intValue() + 30));
                office.setViewCode(IdGen.nextCode(last.getViewCode()));
            } else if(office.getParent() != null) {
                office.setViewCode(office.getParent().getViewCode() + "001");
            }
        }

        model.addAttribute("office", office);
        return "modules/sys/officeForm";
    }

    @RequiresPermissions({"sys:office:edit"})
    @RequestMapping({"createNextNode"})
    @ResponseBody
    public Office createNextNode(Office office) {
        if(StringUtils.isNotBlank(office.getParentCode())) {
            office.setParent((Office)this.officeService.get(office.getParentCode()));
        }

        if(office.getIsNewRecord()) {
            Office where = new Office();
            where.setParentCode(office.getParentCode());
            Office last = (Office)this.officeService.getLastByParentCode(where);
            if(last != null) {
                office.setTreeSort(Integer.valueOf(last.getTreeSort().intValue() + 30));
                office.setViewCode(IdGen.nextCode(last.getViewCode()));
            } else if(office.getParent() != null) {
                office.setViewCode(office.getParent().getViewCode() + "001");
            }
        }

        if(office.getTreeSort() == null) {
            office.setTreeSort(Integer.valueOf(30));
        }

        return office;
    }

    @RequiresPermissions({"sys:office:edit"})
    @PostMapping({"save"})
    @ResponseBody
    public String save(@Validated Office office, String cmd, Model model) {
        this.officeService.save(office);
        return this.renderResult("true", text("保存机构\'\'{0}\'\'成功", new String[]{office.getOfficeName()}));
    }

    @RequiresPermissions({"sys:office:edit"})
    @RequestMapping({"disable"})
    @ResponseBody
    public String disable(Office office) {
        Office where = new Office();
        where.setStatus("0");
        where.setParentCodes("," + office.getId() + ",");
        long count = this.officeService.findCount(where);
        if(count > 0L) {
            return this.renderResult("false", text("该机构包含未停用的子机构！", new String[0]));
        } else {
            office.setStatus("2");
            this.officeService.updateStatus(office);
            return this.renderResult("true", text("停用机构\'\'{0}\'\'成功", new String[]{office.getOfficeName()}));
        }
    }

    @RequiresPermissions({"sys:office:edit"})
    @RequestMapping({"enable"})
    @ResponseBody
    public String enable(Office office) {
        office.setStatus("0");
        this.officeService.updateStatus(office);
        return this.renderResult("true", text("启用机构\'\'{0}\'\'成功", new String[]{office.getOfficeName()}));
    }

    @RequiresPermissions({"sys:office:edit"})
    @RequestMapping({"delete"})
    @ResponseBody
    public String delete(Office office) {
        this.officeService.delete(office);
        return this.renderResult("true", text("删除机构\'\'{0}\'\'成功", new String[]{office.getOfficeName()}));
    }

    @RequiresPermissions({"user"})
    @RequestMapping({"treeData"})
    @ResponseBody
    public List<Map<String, Object>> treeData(String excludeCode, String parentCode, Boolean isAll, String officeTypes, String companyCode, String isShowCode, String isShowFullName, Boolean isLoadUser, String postCode, String roleCode, String ctrlPermi) {
        ArrayList mapList = ListUtils.newArrayList();
//        Office where = new Office();
//        where.setStatus("0");
//        where.setCompanyCode(companyCode);
//        if(isAll == null || !isAll.booleanValue()) {
//            this.officeService.addDataScopeFilter(where, ctrlPermi);
//        }
//
//        List list = this.officeService.findList(where);
        List list = officeDao.getListByOfficeCode(EmpUtils.getOffice().getOfficeCode());

        for(int i = 0; i < list.size(); ++i) {
            Office e = (Office)list.get(i);
            if("0".equals(e.getStatus()) && (!StringUtils.isNotBlank(excludeCode) || !e.getId().equals(excludeCode) && !e.getParentCodes().contains("," + excludeCode + ",")) && (!StringUtils.isNotBlank(parentCode) || e.getOfficeCode().equals(parentCode) && e.getParentCodes().contains("," + parentCode + ",")) && (!StringUtils.isNotBlank(officeTypes) || StringUtils.inString(e.getOfficeType(), officeTypes.split(",")))) {
                HashMap map = MapUtils.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentCode());
                String name = e.getOfficeName();
                if("true".equals(isShowFullName) || "1".equals(isShowFullName)) {
                    name = e.getFullName();
                }

                map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getViewCode(), name));
                map.put("title", e.getFullName());
                if(isLoadUser != null && isLoadUser.booleanValue()) {
                    map.put("isParent", Boolean.valueOf(true));
                    List userList = this.empUserController.treeData("u_", e.getOfficeCode(), e.getOfficeCode(), companyCode, postCode, roleCode, isAll, isShowCode, ctrlPermi);
                    mapList.addAll(userList);
                }

                mapList.add(map);
            }
        }

        return mapList;
    }

    @RequiresPermissions({"sys:office:edit"})
    @RequestMapping({"fixTreeData"})
    @ResponseBody
    public String fixTreeData() {
        if(!UserUtils.getUser().isAdmin()) {
            return this.renderResult("false", "操作失败，只有管理员才能进行修复！");
        } else {
            this.officeService.fixTreeData();
            return this.renderResult("true", "数据修复成功");
        }
    }
}
